package com.example.riji;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.riji.util.MyDiaryDatabase;

public class DiaryDetail extends AppCompatActivity {

    private MyDiaryDatabase mdb;//定义好的数据库操作类
    private EditText d_title;
    private EditText d_body;
    private Button btnSave, btnReturn, btnDelete;
    private int nid;
    private int flag;//1代表修改操作 0代表添加操作

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.diary_detail);
        setTitle("编辑日记");
        d_title = findViewById(R.id.d_title);
        d_body = findViewById(R.id.d_body);
        btnSave = findViewById(R.id.btnSave);
        btnReturn = findViewById(R.id.btnReturn);
        btnDelete = findViewById(R.id.btnDelete);
        //设置监听
        btnSave.setOnClickListener(new mClick());
        btnReturn.setOnClickListener(new mClick());
        btnDelete.setOnClickListener(new mClick());
        mdb = new MyDiaryDatabase(this);
        mdb.openDb();
        //获取传递过来的数据
        flag = this.getIntent().getExtras().getInt("flag");
        if (flag == 1) {
            nid = this.getIntent().getExtras().getInt("ID");
            init(nid);
        }
    }

    private void init(int nid) {
        Cursor cursor = mdb.getData(nid);
        if (cursor != null) {
            if (cursor.moveToNext()) {
                String str1 = cursor.getString(0);
                String str2 = cursor.getString(1);
                d_title.setText(str1);
                d_body.setText(str2);
            }
        }
        cursor.close();
    }

    private class mClick implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            if (view == btnSave) {
                ContentValues values = new ContentValues();
                values.put(mdb.Title, d_title.getText().toString());
                values.put(mdb.Body, d_body.getText().toString());
                if (flag==0){
                    mdb.insertData(values);
                    Toast.makeText(getApplicationContext(), "添加数据成功", Toast.LENGTH_SHORT).show();
                }else if (flag==1){
                    mdb.upData(values,nid);
                    Toast.makeText(getApplicationContext(), "修改数据成功", Toast.LENGTH_SHORT).show();
                }


                //保存操作
            } else if (view == btnReturn) {
                Intent intent = new Intent(DiaryDetail.this, DiaryListActivity.class);
                startActivity(intent);
            } else if (view == btnDelete) {
                //删除操作
                mdb.deleteData(nid);
                //删除记录以后跳回主页面
                Intent intent = new Intent(DiaryDetail.this, DiaryListActivity.class);
                startActivity(intent);
            }
        }
    }
}